package hck;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class ArticleFileParser {
	
	ArrayList<Article> ArticleList = new ArrayList<Article>();
	public static int counter = 0;
	
	public static void main(String[] args) {
		File dirPath = new File(args[0]);
		String boardName = dirPath.getName();
		ArticleFileParser afp = new ArticleFileParser();
		if (args.length != 1)  afp.printUsage();
		afp.listArticleFiles(dirPath, boardName);
		afp.saveArticleList(boardName);
		System.out.println("Iterated " + counter + " times.");
	}
	
	public void printUsage() {
		System.out.println("$ Usage: java ArticleFileParser [dir-path]");
	}

	public void listArticleFiles(File dir, String boardName) {
		System.out.println("Listing article files under " + boardName + "...");
		File[] files = dir.listFiles();
		for (File file : files) {
			if (!(file.getName().equals("log") || file.getName().equals("info"))) {
				System.out.println("filename:: " + file.getName());
				getArticleInfo(file);
				counter++;
			}
		}
	}
	
	public void getArticleInfo(File f) {
		Article a = null;
		BufferedReader br = null;
		String line = null;
		String comment = null;
		String commentator = null;
		String articleID, author, postTime;
		
		System.out.println("Getting article information for (" + f.getName() + ")");
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
			
			/* Here we get the basic info of the article from the first 3 lines */
			articleID = f.getName();
			line = br.readLine(); // 作者: shanyaochung (shan) 看板: StupidClown
			if (line.startsWith("作者: ")) {
				author = line.split(" ")[1]; // shanyaochung
				System.out.println("author:: " + author);
				line = br.readLine(); // 標題: [眼殘] 培養...
				if (line.startsWith("時間: ")) {
					int len = line.length();
					postTime = line.substring(4, len); // Fri Jan  8 19:34:59 2010
					System.out.println("posttime:: " + postTime);
				} else {
					postTime = "No time specified for this article: " + f.getName();
				}
				
				/* Initialize an aricle object with caught attributes */
				a = new Article(articleID, author, postTime);
				
				/* For the rest evaluate the tags for commentators */
				while ((line = br.readLine()) != null) {
					if ((line.startsWith("◆ From: ")) != false) {
						while ((line = br.readLine()) != null) {
							if (line.startsWith("推 ")) {
								commentator = extractCommentator(line, comment);
								if (!(a.getRecommendersList().contains(commentator)) && 
									!(commentator.equals(author)) && 
									!(commentator.equals("not-valid-commentator")))
									a.addRecommender(commentator);
							} else if (line.startsWith("→ ")) {
								commentator = extractCommentator(line, comment);
								if (!(a.getNoncommitersList().contains(commentator)) && 
									!(commentator.equals(author)) &&
									!(commentator.equals("not-valid-commentator")))
									a.addNoncommitter(commentator);
							} else if (line.startsWith("噓 ")) {
								commentator = extractCommentator(line, comment);
								if (!(a.getUnrecommendersList().contains(commentator)) && 
									!(commentator.equals(author)) && 
									!(commentator.equals("not-valid-commentator")))
									a.addUnrecommender(commentator);
							} else if (line.startsWith("◆ From: ")) {
								a.cleanLists();
							}
						}
						
					}
				}
				System.out.println("recom: " + a.getRecommendersList());
				System.out.println("unrec: " + a.getUnrecommendersList());
				System.out.println("nonco: " + a.getNoncommitersList());
			
				/* Add this article to ArticleList */
				ArticleList.add(a);
				
			}
			br.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Getting (" + f.getName() + ")'s article information is done.\n");
	}
	
	public String extractCommentator(String line, String comment) {
		String commentator = null;
		int commentatorPos;
		
		comment = line.split(" ")[1];
		commentatorPos = comment.indexOf(":");
		if (commentatorPos != -1) {
			commentator = comment.substring(0, commentatorPos);
			return commentator;
		} else {
			return "not-valid-commentator";
		}
	}
	
	public void saveArticleList(String boardName) {
		ObjectOutputStream oos;
		
		System.out.println("Saving article list information for board [" + boardName + "]...");
		try {
			oos = new ObjectOutputStream(new FileOutputStream("../data/ser/" + boardName + ".ser"));
			oos.writeObject(ArticleList);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("[" + boardName + "] board's article list is saved...");
	}
	
}
